package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wangmaolin1
 * @date 2023/11/16 21:25
 */
public class _56 {
    public static void main(String[] args) {
        merge(new int[][]{{1,3},{2,6},{8,10},{15,18}});
    }
    public static int[][] merge(int[][] intervals) {
        List<int[]> tempRes = new ArrayList<>();
        Arrays.sort(intervals, (a, b) -> {
            if (a[0] == b[0]) {
                return b[1] - a[1];
            }
            return a[0] - b[0];
        });
        int start = intervals[0][0];
        int end = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            int left = intervals[i][0];
            int right = intervals[i][1];
            if (end >= left && end <= right) {
                end = right;
            }
            if (left >= start && right <= end) {

            }
            if (end == left) {
                end = right;
            }
            if (end < left) {
                tempRes.add(new int[]{start, end});
                start = left;
                end = right;
            }
        }
        tempRes.add(new int[]{start,end});
        int [][]res=new int[tempRes.size()][2];
        for (int i = 0; i < tempRes.size(); i++) {
            res[i]=new int[]{tempRes.get(i)[0],tempRes.get(i)[1]};
        }
        return res;
    }

}
